import psycopg2
import numpy as np
from enum import Enum
from sqlalchemy import create_engine
import pandas as pd
import geopandas as geopd

# * -----------------------------------辅助类与方法------------------------------------------
class FunEnum(Enum):
    JOIN_1 = 1
    JOIN_2 = 2
    JOIN_3 = 3
    JOIN_4 = 4
    JOIN_CIRCLE = 5
    INTERSECT = 6
    SPLIT = 9
    MATCH_SIMPLE = 11
    MATCH_COMPLEX = 12
    MATCH_CHANGE = 13
    ISOLATE = 14
    EXTEND = 15
    CHECK_DANGLE = 16
    CHECK_ISOLATE = 17
    MATCH_MERGED = 18
    MATCH_REMOVE_MULTI = 19
    
class Envelope:

    def __init__(self, xmin=0,ymin=0,xmax=0,ymax=0,crs_code=4326):
        self.xmin = xmin
        self.ymin = ymin
        self.xmax = xmax
        self.ymax = ymax
        self.crs_code = crs_code
        pass

    def ToString(self, sep = ","):
        return f"{self.xmin}{sep}{self.ymin}{sep}{self.xmax}{sep}{self.ymax}{sep}{self.crs_code}"
    

#*-----------------------------------------------------------------------------------------

#*---------------------------------------数据库设置------------------------------------------
# 连接数据库
# HOST = "localhost"
# DATABASE = "map_matching"
# USER = "postgres"
# PASSWORD = "2020"
# PORT = 5432
# ENCODING = "GBK"

HOST = "localhost"
DATABASE = "huining"
USER = "postgres"
PASSWORD = "asdf8833880"
PORT = 5432
ENCODING = "GBK"

# HOST = "localhost"
# DATABASE = "huining"
# USER = "postgres"
# PASSWORD = "123456"
# PORT = 5432

engine = create_engine(f'postgresql+psycopg2://{USER}:{PASSWORD}@{HOST}:{PORT}/{DATABASE}')
conn = psycopg2.connect(host=HOST, database=DATABASE, user=USER, password=PASSWORD)  # 连接数据库

# 数据库的表的设置
SRID = 32649
# 遥感提取路网
PK_COL = "gid"  # 提取路网的主键列名
GEOM_COL = "geom"  # "geometry"  # 提取路网的空间数据列名
LENGTH_COL = "length"
LAYER_COL = "layer"
FUN_COL = "fun"  # 提取路网的标记列列名
SNODEID_COL = "snodeid"
ENODEID_COL = "enodeid"

# 上报路网
LDBM_COL = "ldbm"
MATCH_COL = "match_road"  # 匹配记录字段

# 分割后上报路网
NEW_LDBM = "ldbm"
# *------------------------------------------------------------------------------------------------


# * ----------------------------------------输入输出设置-------------------------------------
## 输入输出
class DataIO:
    # # 预处理模块
    # 输入
    PREPROCESSING_INPUT_RS = "road"
    PREPROCESSING_INPUT_RP_PREFIX = "lx"
    # 输出
    PREPROCESSING_OUTPUT_RP_PREFIX = "lx_split"

    # # 匹配模块
    # 输入
    MATCH_INPUT_RP_PREFIX = "lx_split"
    MATCH_INPUT_RS = "road"
    # 输出
    MATCH_OUTPUT_RS_PREFIX = "match_lx"

    # # 融合模块
    # 输入
    FUSE_INPUT_RP_PREFIX = "lx_split"
    FUSE_INPUT_RS_PREFIX = "match_lx"
    # 输出
    FUSE_OUTPUT_PREFIX = "fuse_lx"
    FUSE_POSTPROCESS_OUTPUT_PREFIX = "fuse_lx2"
    # 结点
    FUSE_NODE = "node_rs"
    
    # 百米桩
    HECTOR_STAKE_PREFIX = "hectometer_stake"
    
## 融合数据保存路径
### 融合过程运算情况与路段划分状况
check_path = "./check" 
check_save_option = True
### 融合结果
result_path = "./result/join"
result_save_option = True
### 融合过程裁剪点搜索过程
image_path = "./result/image"
image_save_option = True

## 输入路网设置
report_road_dir = r"C:\Users\PC\Desktop\路网匹配\项目文档\成果提交\数据\原始数据\上报路网\\"
remotesensing_road_path = r"H:\A1-University\Study\Graduate\P-RS_Road_match\data\road\road\road.shp"
extent_path = r".\extent.shp"
# *------------------------------------------------------------------------------------------------

# *---------------------------------------------运算参数---------------------------------------------------
class MethodjoinCutThresholdSettings:
    distance_threshold = 20
    filter_window_length =27
    x_length = 1000
    derivative_threshold=0.5,
    filter_window__min_length=5
    savgol_filter_polyorder = 3
    check_similarity_buffer = 30
    snap_method = 'move'

    THRESHOLD_SHORTITEM_CLEAR_LENGTH = 20
    THRESHOLD_SHORTITEM_CLEAR_COORDS_NUM = 5

# 常数
CLUSTER_TOLERANCE = 0.001  # 容差
CLUSTER_TOLERANCE_R = CLUSTER_TOLERANCE * np.sqrt(2)
HAUSDORFF_DIST_MAX = 100

ROAD_RANK = {"G": 1, "S": 2, "X": 3, "Y": 4, "C": 5, "N": 9}
R_CLASS_LIST = [x[0] for x in sorted(ROAD_RANK.items(), key=lambda x:x[1], reverse=False)]

THRESHOLD_MATCH_LENGTH = 0.95
THRESHOLD_EXTEND_TOLERANCE = 30
THRESHOLD_BUFFER_COVER = 0.6
# *------------------------------------------------------------------------------------------------


